const { Service } = require('egg');
const myDate = require('../utils/myDate');

class ExamService extends Service {
  async addTestPaper(testPaperInfo) {
    testPaperInfo.create_time = myDate.getNowDate();
    testPaperInfo.update_time = myDate.getNowDate();
    const token = this.ctx.request.header.authorization?.slice(7);
    const userInfo = this.app.jwt.decode(token);
    testPaperInfo.create_by = userInfo.id;
    testPaperInfo.update_by = userInfo.id;
    delete testPaperInfo.id;
    testPaperInfo.score = testPaperInfo.content.reduce((acc, cur) => {
      return acc + cur.sumScore;
    }, 0);
    testPaperInfo.content = JSON.stringify(testPaperInfo.content);
    const res = await this.app.mysql.insert('exam_paper', testPaperInfo);
    if (res.affectedRows) {
      return {
        state: true,
        msg: res.insertId,
      };
    }
    return {
      state: false,
      msg: '添加失败',
    };
  }

  async updateTestPaper(testPaperInfo) {
    const isExit = await this.app.mysql.get('exam_paper', { id: testPaperInfo.id });
    if (!isExit) {
      return {
        state: false,
        msg: '试卷不存在',
      };
    }
    // 保证创建态不被修改
    testPaperInfo.create_time = isExit.create_time;
    testPaperInfo.create_by = isExit.create_by;
    testPaperInfo.update_time = myDate.getNowDate();
    const token = this.ctx.request.header.authorization?.slice(7);
    const userInfo = this.app.jwt.decode(token);
    testPaperInfo.update_by = userInfo.id;
    testPaperInfo.score = testPaperInfo.content.reduce((acc, cur) => {
      return acc + cur.sumScore;
    }, 0);
    testPaperInfo.content = JSON.stringify(testPaperInfo.content);
    const res = await this.app.mysql.update('exam_paper', testPaperInfo);
    if (res.affectedRows > 0) {
      return {
        state: true,
        msg: '更新成功',
      };
    }
    return {
      state: false,
      msg: '更新失败',
    };

  }

  async delTestPaper(ids) {
    const res = await this.app.mysql.query(
      `delete from exam_paper where id in (${ids.join(',')})`
    );
    if (res.affectedRows) {
      return {
        state: true,
        msg: '删除成功',
      };
    }
    return {
      state: false,
      msg: '删除失败',
    };
  }

  async getTestPaper(id) {
    const res = await this.app.mysql.get('exam_paper', { id });
    if (res) {
      res.content = JSON.parse(res.content);
      return res;
    }
    return false;
  }


  async getTestPapers(pageNum, pageSize, name) {
    if (!pageNum) {
      pageNum = 1;
    }
    if (!pageSize) {
      pageSize = 10;
    }
    let where = '';
    if (name) {
      where = `WHERE name LIKE '%${name}%'`;
    }
    const total = await this.app.mysql.count('exam_paper');
    const res = await this.app.mysql.query(`SELECT * FROM exam_paper ${where} LIMIT ?, ?`, [ (pageNum - 1) * pageSize, pageSize ]);
    if (res) {
      return {
        ques: res,
        total,
      };
    }
    return false;
  }

  async addTest(testInfo) {
    testInfo.create_time = myDate.getNowDate();
    testInfo.update_time = myDate.getNowDate();
    const token = this.ctx.request.header.authorization?.slice(7);
    const userInfo = this.app.jwt.decode(token);
    testInfo.create_by = userInfo.id;
    testInfo.update_by = userInfo.id;
    const res = await this.app.mysql.insert('exam', testInfo);
    if (res.affectedRows) {
      return true;
    }
    return false;
  }
}

module.exports = ExamService;

